package com.zhuiyun.project.api.memberUserManagement.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhuiyun.project.annotation.OptLog;
import com.zhuiyun.project.api.coachmanagement.service.CoachManagementService;
import com.zhuiyun.project.api.memberTypeManagement.entity.MemberCardNumber;
import com.zhuiyun.project.api.memberTypeManagement.entity.MemberTypeManagement;
import com.zhuiyun.project.api.memberTypeManagement.model.MemberTypeManagementModel;
import com.zhuiyun.project.api.memberTypeManagement.model.MemberTypeModel;
import com.zhuiyun.project.api.memberUserManagement.entity.MemberUserManagement;
import com.zhuiyun.project.api.memberUserManagement.mapper.MemberUserManagementMapper;
import com.zhuiyun.project.api.memberUserManagement.model.MemberUserAndTypeModel;
import com.zhuiyun.project.api.memberUserManagement.model.MemberUserModel;
import com.zhuiyun.project.api.memberUserManagement.service.MemberUserManagementService;

import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.DateTimeUtils;
import com.zhuiyun.project.utils.constants.CommonConstants;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * @ClassName MemberUserManagementControllrt
 * @Description TODO 会员用户管理控制层
 * @Author WX
 * @Date 2023/11/3 8:30
 **/
@RestController
@Api(tags = "会员用户管理")
@Slf4j
@CrossOrigin
@RequestMapping("/memberUser")
public class MemberUserManagementController {

    @Autowired
    private MemberUserManagementService memberUserManagementService;

    @Resource
    MemberUserManagementMapper memberUserManagementMapper;

    @Autowired
    private CoachManagementService coachManagementService;

    /**
    * @Author WX
    * @Description 获取会员用户信息
    * @Date 2023/11/3 10:15
    * @Return
    * @param pageRequest memberUserModel
    */
    @ApiOperation(value = "获取会员用户信息")
    @OptLog(modular = "会员用户管理", type = CommonConstants.SELECT, describe = "select")
    @GetMapping("/getMemberUser")
    public CommonResult getMemberType(PageRequest pageRequest, MemberUserModel memberUserModel) {
        return CommonResult.ok(memberUserManagementService.getMemberType(pageRequest,memberUserModel));
    }
    /**
     * @Author WX
     * @Description 获取会员用户信息
     * @Date 2023/11/3 10:15
     * @Return
     * @param
     */
    @ApiOperation(value = "获取会员用户信息")
    @OptLog(modular = "会员用户管理", type = CommonConstants.SELECT, describe = "select")
    @GetMapping("/selectByPhone")
    public CommonResult selectByPhone(MemberUserAndTypeModel memberUserAndTypeModel) {
        return CommonResult.ok(memberUserManagementService.selectByPhone(memberUserAndTypeModel));
    }
    /**
     * @Author WX
     * @Description 获取会员用户信息
     * @Date 2023/11/3 10:15
     * @Return
     * @param
     */
    @ApiOperation(value = "获取会员用户信息")
    @OptLog(modular = "会员用户管理", type = CommonConstants.SELECT, describe = "select")
    @GetMapping("/selectByPhoneOnly")
    public CommonResult selectByPhoneOnly(String phone) {
        return CommonResult.ok(memberUserManagementService.selectByPhoneOnly(phone));
    }
    /**
    * @Author wx
    * @Description 查询会员类型信息
    * @Date 2023/11/6 9:20
    * @Return
     */
    @ApiOperation(value = "查询会员类型信息")
    @OptLog(modular = "会员用户管理", type = CommonConstants.SELECT, describe = "select")
    @GetMapping("/getMemberList/{venueUniqueIdentifier}")
    public CommonResult getMemberList (@PathVariable("venueUniqueIdentifier") String venueUniqueIdentifier) {
        return CommonResult.ok(memberUserManagementService.getMemberList(venueUniqueIdentifier));
    }

    /**
    * @Author wx
    * @Description 新增会员类型信息
    * @Date 2023/11/6 10:18
    * @Return
    * @param memberUserAndTypeModel file
    */
    @ApiOperation(value = "新增会员类型信息")
    @OptLog(modular = "会员用户管理", type = CommonConstants.SELECT, describe = "add")
    @PostMapping("/addMember")
    public CommonResult addMember ( MemberUserAndTypeModel memberUserAndTypeModel,MultipartFile file)  throws CommonException {
//        boolean b =memberUserManagementService.addMember(memberUserAndTypeModel,file);
        return CommonResult.ok(memberUserManagementService.addMember(memberUserAndTypeModel,file));
    }


    /**
    * @Author wx
    * @Description 根据id查询单个会员的数据
    * @Date 2023/11/7 8:29
    * @Return
    * @param memberUserManagement
    */
    @ApiOperation(value = "根据id查询单个会员的数据")
    @OptLog(modular = "会员用户管理", type = CommonConstants.SELECT, describe = "select")
    @PutMapping("/getById")
    public CommonResult getById(@RequestBody MemberUserManagement memberUserManagement) {
        Integer id = memberUserManagement.getId();
        String venueUniqueIdentifier= memberUserManagement.getVenueUniqueIdentifier();
        MemberUserAndTypeModel memberUserAndTypeModel = memberUserManagementService.getById(id,venueUniqueIdentifier);
        return CommonResult.ok(memberUserAndTypeModel);
    }

    /**
    * @Author wx
    * @Description 通过id修改会员用户信息
    * @Date 2023/11/7 9:40
    * @Return
    * @param memberUserAndTypeModel,file
    */
    @ApiOperation(value = "通过id修改会员用户信息")
    @OptLog(modular = "会员用户管理", type = CommonConstants.UPDATE, describe = "update")
    @PutMapping("/updateMemberById")
    public CommonResult updateMemberById ( MemberUserAndTypeModel memberUserAndTypeModel,MultipartFile file) throws CommonException {
//        boolean b = memberUserManagementService.updateMemberById(memberUserAndTypeModel,file);
        return CommonResult.ok(memberUserManagementService.updateMemberById(memberUserAndTypeModel,file));
    }
    /**
     * @Author wx
     * @Description 通过id修改会员用户信息
     * @Date 2023/11/7 9:40
     * @Return
     * @param memberUserAndTypeModel,file
     */
    @ApiOperation(value = "通过id修改会员用户信息")
    @OptLog(modular = "会员用户管理", type = CommonConstants.UPDATE, describe = "update")
    @PostMapping("/updateMemberByIds")
    public CommonResult updateMemberByIds (@RequestBody MemberUserAndTypeModel memberUserAndTypeModel){
//        boolean b = memberUserManagementService.updateMemberById(memberUserAndTypeModel,file);
        return CommonResult.ok(memberUserManagementService.updateMemberByIds(memberUserAndTypeModel));
    }



    @ApiOperation(value = "通过id修改身高体重")
    @OptLog(modular = "会员用户管理", type = CommonConstants.UPDATE, describe = "update")
    @PostMapping("/updateMemberInfoById")
    public CommonResult updateMemberInfoById ( @RequestBody MemberUserAndTypeModel memberUserAndTypeModel){
        return CommonResult.ok(memberUserManagementService.updateMemberInfoById(memberUserAndTypeModel));
    }

    /*
    * @Author xy
    * @Description todo 学员会员信息查询
    * @Date 2023/11/24 10:51
    * @Return
    * @param
    */
    @GetMapping("/selectAllMember")
    @ApiOperation(value = "学员会员信息查询")
    @OptLog(modular = "会员用户管理",type = CommonConstants.SELECT,describe = "学员会员信息查询")
    public CommonResult selectAllMember(@RequestParam Integer userId,@RequestParam String userName){
        // 根据前端传过来的userId（会员id）来查询会员表id符合的数据
        QueryWrapper<MemberUserManagement> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",userId);
        queryWrapper.eq("member_user_real_name",userName);
        List<MemberUserManagement> memberUserManagements = memberUserManagementMapper.selectList(queryWrapper);
        return CommonResult.ok(memberUserManagements);
    }

    /*
    * @Author xy
    * @Description todo 会员头像查询
    * @Date 2023/11/28 18:56
    * @Return
    * @param
    */
    @GetMapping("/selectMemberHeadPortrait")
    @ApiOperation(value = "会员头像查询")
    @OptLog(modular = "会员用户管理",type = CommonConstants.SELECT,describe = "会员头像查询")
    public CommonResult selectMemberHeadPortrait(@RequestParam Integer userId){
        // 根据前端传过来的userId（会员id）来查询会员表id符合的数据
        QueryWrapper<MemberUserManagement> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",userId);
        List<MemberUserManagement> memberUserManagements = memberUserManagementMapper.selectList(queryWrapper);
        return CommonResult.ok(memberUserManagements);
    }

    /**
    * @Author wx
    * @Description 刷卡确认后条件判断删除任务
    * @Date 2023/12/3 18:51
    * @Return
    * @param memberUserManagement
    */
    @ApiOperation(value = "刷卡确认后条件判断删除任务")
    @OptLog(modular = "会员用户管理", type = CommonConstants.SELECT, describe = "刷卡确认后条件判断删除任务")
    @PutMapping("/deleteNumberTimes")
    public CommonResult deleteNumberTimes(@RequestBody MemberUserManagement memberUserManagement) {
        String memberUserPhone = memberUserManagement.getMemberUserPhone();
        String venueUniqueIdentifier= memberUserManagement.getVenueUniqueIdentifier();

        return CommonResult.ok(memberUserManagementService.getByMemberUserPhone(memberUserPhone,venueUniqueIdentifier));
    }

    /**
     * @Author wx
     * @Description 根据invitationCode查询单教练的数据
     * @Date 2023/12/3 18:51
     * @Return
     * @param venueUniqueIdentifier
     */
    @ApiOperation(value = "根据invitationCode查询单教练的数据")
    @OptLog(modular = "会员用户管理", type = CommonConstants.SELECT, describe = "根据invitationCode查询单教练的数据")
    @GetMapping("/selectByInvitationCode")
    public CommonResult selectByInvitationCode(@RequestParam String invitationCode, @RequestParam String venueUniqueIdentifier,@RequestParam Integer id) {
        // 处理请求逻辑
        System.out.println(invitationCode);
        System.out.println(venueUniqueIdentifier);
        System.out.println(id);
        return CommonResult.ok(coachManagementService.selectByInvitationCode(id,invitationCode,venueUniqueIdentifier));
    }

    /**
    * @Author wx
    * @Description 根据用户ID查询单教练的数据
    * @Date 2023/12/4 10:19
    * @Return
    * @param venueUniqueIdentifier
    */
    @ApiOperation(value = "根据用户ID查询单教练的数据")
    @OptLog(modular = "会员用户管理", type = CommonConstants.SELECT, describe = "根据用户ID查询单教练的数据")
    @GetMapping("/selectCoachByUserId")
    public CommonResult selectCoachByUserId( @RequestParam String venueUniqueIdentifier,@RequestParam Integer id) {
        // 处理请求逻辑
        System.out.println(venueUniqueIdentifier);
        System.out.println(id);
        return CommonResult.ok(coachManagementService.selectCoachByUserId(id,venueUniqueIdentifier));
    }

    /**
    * @Author wx
    * @Description 根据邀请码查询单教练的数据
    * @Date 2023/12/4 10:48
    * @Return
    * @param invitationCode
    */
    @ApiOperation(value = "根据邀请码查询单教练的数据")
    @OptLog(modular = "会员用户管理", type = CommonConstants.SELECT, describe = "根据邀请码查询单教练的数据")
    @GetMapping("/selectCoachByInvitationCode")
    public CommonResult selectCoachByInvitationCode( @RequestParam String venueUniqueIdentifier,@RequestParam String invitationCode) {
        // 处理请求逻辑
        System.out.println(venueUniqueIdentifier);
        System.out.println(invitationCode);
        return CommonResult.ok(coachManagementService.selectCoachByInvitationCode(invitationCode,venueUniqueIdentifier));
    }

    /**
    * @Author wx
    * @Description 根据邀请码查询邀请码申请的数据
    * @Date 2023/12/5 9:26
    * @Return
    * @param venueUniqueIdentifier
    */
    @ApiOperation(value = "根据邀请码查询邀请码申请的数据")
    @OptLog(modular = "会员用户管理", type = CommonConstants.SELECT, describe = "根据邀请码查询邀请码申请的数据")
    @GetMapping("/selectCoachApplyByInvitationCode")
    public CommonResult selectCoachApplyByInvitationCode( @RequestParam String venueUniqueIdentifier,@RequestParam String invitationCode,@RequestParam Integer memberUserId) {
        // 处理请求逻辑
        System.out.println(venueUniqueIdentifier);
        System.out.println(invitationCode);
        return CommonResult.ok(coachManagementService.selectCoachApplyByInvitationCode(invitationCode,venueUniqueIdentifier,memberUserId));
    }

    /**
    * @Author wx
    * @Description 添加用户的教练邀请码
    * @Date 2023/12/5 18:42
    * @Return
    * @param memberUserId
    */
    @ApiOperation(value = "添加用户的教练邀请码")
    @OptLog(modular = "会员用户管理", type = CommonConstants.UPDATE, describe = "添加用户的教练邀请码")
    @GetMapping("/updateCoachCode")
    public CommonResult updateCoachCode( @RequestParam String coachCode,@RequestParam Integer memberUserId) throws CommonException {
        // 处理请求逻辑
        System.out.println(coachCode);
        System.out.println(memberUserId);
        return CommonResult.ok(coachManagementService.updateCoachCode(coachCode,memberUserId));
    }

    /**
    * @Author wx
    * @Description 教练端查询会员信息
    * @Date 2023/12/12 9:57
    * @Return
    * @param id
    */
    @ApiOperation(value = "教练端查询会员信息")
    @OptLog(modular = "会员用户管理", type = CommonConstants.SELECT, describe = "教练端查询会员信息")
    @GetMapping("/selectUserCase")
    public CommonResult selectUserCase (@RequestParam String venueUniqueIdentifier,@RequestParam Integer id,@RequestParam String coachId){
        return CommonResult.ok(memberUserManagementService.selectUserCase(venueUniqueIdentifier,id,coachId));
    }

    /**
    * @Author wx
    * @Description 查询用户案例图片
    * @Date 2023/12/12 10:21
    * @Return
    * @param id
    */
    @ApiOperation(value = "查询用户案例图片")
    @OptLog(modular = "会员用户管理", type = CommonConstants.SELECT, describe = "查询用户案例图片")
    @GetMapping("/selectUserInfoImage")
    public CommonResult selectUserInfoImage (@RequestParam Integer id,@RequestParam Integer coachId){
        return CommonResult.ok(memberUserManagementService.selectUserInfoImage(id,coachId));
    }

    /**
    * @Author wx
    * @Description 查询场管名称
    * @Date 2023/12/13 8:51
    * @Return
    * @param venueUniqueIdentifier
    */
    @ApiOperation(value = "查询场管名称")
    @OptLog(modular = "会员用户管理", type = CommonConstants.SELECT, describe = "查询场管名称")
    @GetMapping("/selectVenueName")
    public CommonResult selectVenueName (@RequestParam String venueUniqueIdentifier){
        return CommonResult.ok(memberUserManagementService.selectVenueName(venueUniqueIdentifier));
    }

    /**
    * @Author wx
    * @Description 读卡回显信息
    * @Date 2023/12/22 9:03
    * @Return
    * @param venueUniqueIdentifier
    */
    @ApiOperation(value = "读卡回显信息")
    @OptLog(modular = "会员用户管理", type = CommonConstants.SELECT, describe = "读卡回显信息")
    @GetMapping("/selectUserInfoByNumid")
    public CommonResult selectUserInfoByNumid (@RequestParam String venueUniqueIdentifier,@RequestParam String membershipCardNumber){
        return CommonResult.ok(memberUserManagementService.selectUserInfoByNumid(venueUniqueIdentifier,membershipCardNumber));
    }

//    /**
////    * @Author wx
////    * @Description 查询入会人数
////    * @Date 2023/12/27 20:33
////    * @Return
////    * @param venueUniqueIdentifier
////    */
////    @ApiOperation(value = "查询入会人数")
////    @OptLog(modular = "会员用户管理", type = CommonConstants.SELECT, describe = "查询入会人数")
////    @GetMapping("/selectOnlineStatuss/{venueUniqueIdentifier}")
////    public CommonResult selectOnlineStatuss (@PathVariable("venueUniqueIdentifier") String venueUniqueIdentifier){
////        return CommonResult.ok(memberUserManagementService.selectOnlineStatuss(venueUniqueIdentifier));
////    }

    /**
    * @Author 胡家浩
    * @Description 查询当天的入场人数
    * @Date 2024/5/16 16:18
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param venueUniqueIdentifier
* @param currentDate
    */
    @ApiOperation(value = "查询会员当天入场人数")
    @OptLog(modular = "会员用户管理", type = CommonConstants.SELECT,describe = "查询会员当天入场人数")
    @GetMapping("/selectEntranceNumber")
    public CommonResult selectEntranceNumber (@RequestParam String venueUniqueIdentifier,@RequestParam String currentDate){
        String currentDate1 = currentDate.substring(0, 10);
        CommonResult result = memberUserManagementService.selectEntranceNumber(venueUniqueIdentifier, currentDate1);
        return CommonResult.ok(result);
    }

    /**
    * @Author 胡家浩
    * @Description 查询用户是否入场
    * @Date 2024/5/16 21:30
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param memberCardNumber
    */
    @ApiOperation(value = "查询会员是否入场")
    @OptLog(modular = "会员用户管理", describe = "查询会员是否入场")
    @PostMapping("/userIfEntrance")
    public CommonResult userIfEntrance (@RequestBody MemberCardNumber memberCardNumber){
        memberCardNumber.setUpdateTime(new Date());
        CommonResult result = memberUserManagementService.userIfEntrance(memberCardNumber);
        return result;
    }



    /**
    * @Author wx
    * @Description 会员入场
    * @Date 2023/12/28 8:16
    * @Return
    * @param id
    */
    @ApiOperation(value = "会员入场")
    @OptLog(modular = "会员用户管理", type = CommonConstants.SELECT, describe = "会员入场")
    @GetMapping("/intoVenuById")
    public CommonResult selectUserInfoByNumid (@RequestParam String venueUniqueIdentifier,@RequestParam Integer id){
        return CommonResult.ok(memberUserManagementService.intoVenuById(venueUniqueIdentifier,id));
    }

    /**
     * @Author wx
     * @Description 会员离场
     * @Date 2023/12/28 8:16
     * @Return
     * @param id
     */
    @ApiOperation(value = "会员离场")
    @OptLog(modular = "会员用户管理", type = CommonConstants.SELECT, describe = "会员离场")
    @GetMapping("/outVenuById")
    public CommonResult outVenuById (@RequestParam String venueUniqueIdentifier,@RequestParam Integer id){
        return CommonResult.ok(memberUserManagementService.outVenuById(venueUniqueIdentifier,id));
    }

    /**
    * @Author 胡家浩
    * @Description 查询会员入场次数
    * @Date 2024/5/20 14:38
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param venueUniqueIdentifier
* @param year
* @param month
* @param week
    */
    @ApiOperation("查询会员入场次数")
    @OptLog(modular = "会员用户管理", type = CommonConstants.SELECT, describe = "查询会员入场次数")
    @GetMapping("/getMemberEntranceTimes")
    public CommonResult getMemberEntranceTimes (@RequestParam String venueUniqueIdentifier,
                                                @RequestParam(value = "year", required = false) String year,
                                                @RequestParam(value = "month", required = false)String month,
                                                @RequestParam(value = "week", required = false)String week){
        return CommonResult.ok(memberUserManagementService.getMemberEntranceTimes(venueUniqueIdentifier,year,month,week));
    }

    /**
    * @Author 胡家浩
    * @Description 统计会员办卡情况
    * @Date 2024/5/21 17:15
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param venueUniqueIdentifier
    * @param year
    * @param month
    * @param week
    */
    @ApiOperation("统计会员办卡情况")
    @OptLog(modular = "会员用户管理",type = CommonConstants.SELECT, describe = "统计会员办卡情况")
    @GetMapping("/getMemberUserCardDate")
    public CommonResult getMemberUserCardDate (@RequestParam String venueUniqueIdentifier,
                                               @RequestParam(value = "year", required = false) String year,
                                               @RequestParam(value = "month", required = false)String month,
                                               @RequestParam(value = "week", required = false)String week){
        return CommonResult.ok(memberUserManagementService.getMemberUserCardDate(venueUniqueIdentifier,year,month,week));
    }
}
